<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>链表</title>
</head>
<body>
<script>
  /*佟金龙*/
  class Node {
    constructor({val, next}) {
      this.val = val;
      this.next = next;
    }
  }

  let _head = Symbol('_head');

  // {_head:{}}  key : _head
  // obj[_head] = {} : Symbol('_head')
  class NodeList {
    constructor() {
      Object.defineProperties(this, {
        [_head]: {
          writable: true,
          value: new Node({val: null, next: null}),
          enumerable: false,
          configurable: false
        }
      })
    }

    /*祝成*/
    find(index) {
      let i = 0;
      let el = this._head;
      while (el && i !== index) {
        el = el.next;
        i++
      }
      return el
    }

    /*杨云涵*/
    update(val, index) {
      let findNode = this.find(index);
      findNode.val = val;
    }

    /*杨云涵*/
    delete(index) {
      let n = this._head;
      let findNode = this.find(index);
      if (index === 0) {
        this._head = findNode.next;
        return
      }
      let preIndex = this.find(index - 1);
      preIndex.next = findNode.next;
    }

    insert(val, index) {
      let  nnode = new Node({val, next: null})
      if(!this._head){
        this._head=nnode
        return
      }
      if(index===0){
        nnode.next=this._head
        this._head=nnode
        return
      }
      if(!index){
        let last=this.find(this._head.length-1)
        last.next=nnode
        return;
      }
      let pre =this.find(index-1)
      nnode.next=pre.next
      pre.next=nnode
    }
  }

  let nodeList = new NodeList();
  console.log(nodeList);

</script>
</body>
</html>
